package com.jd.basic.controller;

import com.jd.basic.config.BusinessException;
import com.jd.basic.dto.SmsCodeDto;
import com.jd.basic.service.IVerifyService;
import com.jd.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyService verifyService;




    /**
     * 获取图片验证码
     * @param key
     * @return
     */
    @RequestMapping("/image/{key}")
    public AjaxResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyService.getImageCode(key);
            System.out.println(base64Str);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage("获取验证码失败！"
                    + e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试。");
        }
    }

    /**
     * 生成手机验证码
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto ){
        try {
            verifyService.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试");
        }

    }
    /**
     * 微信绑定手机 - 验证码
     * @param smsCodeDto
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto ){
        try {
            verifyService.binderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试");
        }

    }

}
